package com.guigu.p1;

import java.util.ArrayList;
import java.util.Scanner;

public class CustomerView {
    ArrayList<Customer> arr = new ArrayList<Customer>();
    public void enterview()
    {
        Scanner scan = new Scanner(System.in);
        boolean isflag = true;
        while(isflag)
        {
            System.out.println("-----------------客户信息管理系统-----------------");
            System.out.println("                   1 添加客户");
            System.out.println("                   2 修改客户");
            System.out.println("                   3 删除客户");
            System.out.println("                   4 客户列表");
            System.out.println("                   5 退出");
            System.out.print("请选择(1-5):");
            String menu = scan.nextLine();
            switch (menu)
            {
                case "1":
                    addCustomer(arr);
                    break;
                case "2":
                    changeCustomer(arr);
                    break;
                case "3":
                    deleteCustomer(arr);
                    break;
                case "4":
                    listCustomer(arr);
                    break;
                case "5":
                    System.out.print("确认是否退出(Y/N):");
                    String isexit = scan.next();
                    if("Y".equals(isexit))
                    {
                        isflag = false;
                    }
                    System.out.println();
            }
        }
    }

    /**
     * 添加操作
     * @param arr
     */
    public void addCustomer(ArrayList<Customer> arr)
    {
        Scanner scan = new Scanner(System.in);
        System.out.println("---------------------添加客户----------------------");
        System.out.print("姓名：");
        String name = scan.nextLine();
        System.out.print("年龄：");
        String  age = scan.nextLine();
        System.out.print("性别：");
        String gender = scan.nextLine();
        System.out.print("电话号码：");
        String phone = scan.nextLine();
        System.out.print("电子邮箱：");
        String email = scan.nextLine();
        Customer customer = new Customer(name,age,gender,phone,email);
        arr.add(customer);
        System.out.println("---------------------添加客户成功-----------------------");
    }

    /**
     * 修改客户操作
     * @param arr
     */
    public void changeCustomer(ArrayList<Customer> arr)
    {
        Scanner scan = new Scanner(System.in);
        System.out.println("-----------------修改客户------------------");
        System.out.print("请输入您要查询客户的序号：");
        int index = scan.nextInt();
        int number = arr.size();
        boolean isfalg = true;
        while(isfalg)
        {
            if(index<0 || index>number)
            {
                System.out.println("查询号错误，请重新输入");
                isfalg  = false;
            }
            if(index>0&&index<=number)
            {
                System.out.print("姓名：");
                String jieshou = scan.nextLine();
                String name = scan.nextLine();
                System.out.print("年龄：");
                String  age = scan.nextLine();
                System.out.print("性别：");
                String gender = scan.nextLine();
                System.out.print("电话号码：");
                String phone = scan.nextLine();
                System.out.print("电子邮箱：");
                String email = scan.nextLine();
                arr.set(index-1,new Customer(name,age,gender,phone,email));
                isfalg = false;
                System.out.println("--------------------修改客户成功---------------------");
            }
        }
    }

    /**
     * 删除客户操作
     */
    public void deleteCustomer(ArrayList<Customer> arr)
    {
        Scanner scan = new Scanner(System.in);
        System.out.println("------------------------删除客户--------------------------");
        System.out.print("请输入您要删除客户的序号");
        int index = scan.nextInt();
        int number = arr.size();
        boolean isfalg = true;
        while (isfalg)
        {
            if(index<0 || index>number)
            {
                System.out.println("删除序号错误，请重新输入");
                isfalg = false;
            }
            if(index>0&&index<=number)
            {
                arr.remove(index-1);
                System.out.println("---------------------------删除客户成功------------------------");
                isfalg = false;
            }
        }
    }
    /**
     * 查看客户列表操作
     * @param arr
     */
    public void listCustomer(ArrayList<Customer> arr)
    {
        System.out.println("--------------------------客户列表------------------------");
        if(arr.size()==0)
        {
            System.out.println("没有客户记录！");
            System.out.println("--------------------------客户列表完成------------------------");
            return;
        }
        System.out.println("编号\t姓名\t年龄\t性别\t电话号码\t\t电子邮箱");
        for(int i=0;i<arr.size();i++)
        {
            Customer o = arr.get(i);
            System.out.println((i+1)+"\t\t"+o.toString());
        }
        System.out.println("--------------------------客户列表完成------------------------");
    }
}
